/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.gbvbahia.vendas.controllers;

import br.com.gbvbahia.vendas.beans.ChartBean;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

/**
 *
 * @author Guilherme
 */
@Named(value = "chartController")
@RequestScoped
public class ChartController implements Serializable {

    private static final Integer PERIODO = 5;
    @EJB
    private ChartBean chartBean;
    private CartesianChartModel categoryModel;

    /**
     * Creates a new instance of ChartController
     */
    public ChartController() {
    }

    //====================
    // Iniciadores        
    //====================
    @PostConstruct
    public void init() {
        atualizarBarrChart();
    }

    //====================
    //Métodos de Negócio  
    //====================
    private void atualizarBarrChart() {
        Date[] datas = periodoInformacao();
        categoryModel = new CartesianChartModel();
        //Busca todas as contas cartão de credito
        Map<Date, Double> valor = new LinkedHashMap<Date, Double>();
        for (Date date : datas) {//Busca todos os pagamentos de cartão por data do periodo
            Double v = chartBean.totalVendasPorPeriodo(date);
            valor.put(date, v);
        }
        final SimpleDateFormat sdf = getFormat("MMM/yy");
        ChartSeries serie = new ChartSeries("Vendas");
        for (Date data : valor.keySet()) {
            serie.set(sdf.format(data), valor.get(data));
        }
        categoryModel.addSeries(serie);//Adiciona a linha no gráfico
    }

    /**
     * Cria um date format com o pattern solitado com timezone Brasil.
     *
     * @param pattern dd/MM/yyyy ou dd/MM/yy ...
     * @return SimpleDateFormat.
     */
    public static SimpleDateFormat getFormat(String pattern) {
        SimpleDateFormat sd = new SimpleDateFormat(pattern,
                new Locale("pt", "BR"));
        sd.setTimeZone(TimeZone.getTimeZone("GMT-03:00"));
        return sd;
    }

    private Date[] periodoInformacao() {
        Date[] toReturn = new Date[PERIODO + 1];
        int j = 0;
        for (int i = (PERIODO * (-1)); i <= 0; i++) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, i);
            toReturn[j++] = c.getTime();
        }
        return toReturn;
    }

    //====================
    //Getters AND Setters
    //====================
    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public void setCategoryModel(CartesianChartModel categoryModel) {
        this.categoryModel = categoryModel;
    }
}
